package com.zuipin.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zuipin.entity.BalanceRecord;
import com.zuipin.entity.GrowthValRecord;
import com.zuipin.entity.MemberAccount;
import com.zuipin.entity.OrderInfo;
import com.zuipin.entity.SysUser;
import com.zuipin.enums.GrowthValType;
import com.zuipin.enums.RecordSearchTime;
import com.zuipin.framework.annotation.Inject;
import com.zuipin.framework.annotation.Json;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.PageResult;
import com.zuipin.mapper.OrderInfoMapper;
import com.zuipin.service.IGrowthValRecordService;
import com.zuipin.service.IMemberAccountService;
import com.zuipin.service.ISysUserService;
import com.zuipin.util.Pagination;
import com.zuipin.vo.GrowthValAndRecordVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author zhuangjiajian
 * @time 2017-7-29 15:55:41
 */
@Api(value = "会员成长值记录", description = "会员成长值记录")
@RestController
@RequestMapping(value = "/member/growthValRecord")
public class GrowthValRecordController {
	@Resource
	private IGrowthValRecordService	growthValRecordService;
	
	@Resource
	private OrderInfoMapper			orderInfoMapper;
	@Resource
	private IMemberAccountService	memberAccountService;
	@Resource
	private ISysUserService			sysUserService;
	
	private static Logger			log	= LoggerFactory.getLogger(GrowthValRecordController.class);
	
	@Json(type = BalanceRecord.class, exclude = "remark")
	@RequestMapping(value = "/searchGrowthValRecord", method = RequestMethod.GET)
	@ApiOperation(value = "查询成长值记录 备注：购物送积分（订单号：123456)  remark保存着购物送积分字段  describes保存 订单号字段  oddNumbers保存单号字段", notes = " 备注：购物送积分（订单号：123456)  remark保存着购物送积分字段  describes保存 订单号字段  oddNumbers保存单号字段 查询成长值记录 成长值类型：购物送成长值，退款扣除成长值，签到送成长值，分享送成长值，评价送成长值，后台调整会员等级成长值变更")
	@Valids({ @Valid(name = "memberId", minValue = 1), @Valid(name = "sysId", minValue = 1),
			@Valid(name = "growthValType", required = false, regex = "'BUY_USE|REFUND|SIGN_IN|SHARE|EVALUATE|BACK_ADD|BACK_REMOVE"),
			@Valid(name = "searchTime", required = true, regex = "THREE_MONTH_AGO|ONE_YEAR_AGO|ALL_AGO") })
	@ApiImplicitParams({ @ApiImplicitParam(name = "memberId", value = "会员ID", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "searchTime", value = "THREE_MONTH_AGO（三个月前）|ONE_YEAR_AGO（一年内）|ALL_AGO（全部时间）", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "growthValType", value = "成长值类型", required = false, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "page.pageNumber", value = "页码", required = true, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "page.pageSize", value = "单页记录数", required = true, dataType = "integer", paramType = "query") })
	public PageResult<GrowthValAndRecordVo> search(@RequestParam(required = false) GrowthValType growthValType, @RequestParam Long memberId, @RequestParam Long sysId,
			@RequestParam RecordSearchTime searchTime, @Inject @ApiIgnore Pagination page) {
		// 当前积分和总记录
		PageResult<GrowthValAndRecordVo> result = new PageResult<>();
		GrowthValAndRecordVo growthValAndRecordVo = new GrowthValAndRecordVo();
		GrowthValRecord growthValRecord = new GrowthValRecord();
		growthValRecord.setGrowthValType(growthValType);
		growthValRecord.setSysId(sysId);
		growthValRecord.setMemberId(memberId);
		growthValRecord.setSearchTime(searchTime.getKey());
		
		// 获取当前积分
		MemberAccount ma = memberAccountService.findByMemberIdAndSysId(memberId, sysId);
		if (ma != null) {
			growthValAndRecordVo.setGrowthVal(ma.getGrowthValue() == null ? 0 : ma.getGrowthValue());
		} else {
			growthValAndRecordVo.setGrowthVal(0);
		}
		
		// 获取成长值记录
		List<GrowthValRecord> list = growthValRecordService.findListByPage(growthValRecord, page);
		if (list != null && list.size() > 0) {
			List<Long> sysUserIdList = new ArrayList<Long>();
			// 填充订单ID给前台
			for (GrowthValRecord gr : list) {
				sysUserIdList.add(gr.getCreateId());
				OrderInfo orderInfo = orderInfoMapper.findOrderInfoByOrderNo(gr.getOddNumbers());
				if (orderInfo != null) {
					gr.setOrderId(orderInfo.getId());
				}
			}
			List<SysUser> sysUserList = sysUserService.findByIds(sysUserIdList);
			Map<Long, String> map = new HashMap<Long, String>();
			for (SysUser su : sysUserList) {
				map.put(su.getId(), su.getRealName());
			}
			for (GrowthValRecord gr : list) {
				gr.setCreateName(map.get(gr.getCreateId()));
			}
			growthValAndRecordVo.setRecords(list);
			result.setTotal_record(list.size());
			growthValAndRecordVo.setRecords(list);
			result.setData(growthValAndRecordVo);
		} else {
			result.setData(growthValAndRecordVo);
			result.setTotal_record(0);
		}
		
		result.setTotal_record(page.getTotalRecord());
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	/*	@RequestMapping(value = "/addIntegralRecord", method = RequestMethod.POST)
		@ApiOperation(value = "积分变更接口-wuyicheng", notes = "积分变更接口-wuyicheng")
		@Valids({ @Valid(name = "memberId", minValue = 1), @Valid(name = "sysId", minValue = 1), @Valid(name = "alterPoint", regex = RegexUtils.INTEGER),
				@Valid(name = "integralType", regex = "PAY|GIFT_CARD|REFUND|BUY_USE"), })
		@ApiImplicitParams({ @ApiImplicitParam(name = "memberId", value = "会员ID", required = true, dataType = "long", paramType = "query"),
				@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
				@ApiImplicitParam(name = "createId", value = "创建人ID", required = false, dataType = "long", paramType = "query"),
				@ApiImplicitParam(name = "updateId", value = "修改人ID", required = false, dataType = "long", paramType = "query"),
				@ApiImplicitParam(name = "alterPoint", value = "修改的积分", required = true, dataType = "bigDecimal", paramType = "query"),
				@ApiImplicitParam(name = "integralType", value = "'REGISTER','SIGN_IN','GET_SHOPPING','USE_SHOPPING','ORDER_RECYCLING','OTHERS'类型：会员注册送积分  会员签到  购物赠送积分 购物积分抵扣  回收赠送积分 其他", required = true, dataType = "string", paramType = "query"),
				@ApiImplicitParam(name = "remark", value = "备注", required = false, dataType = "string", paramType = "query"), })
		
		public Result<Map<String, Object>> addIntegralRecord(@RequestParam Long memberId, @RequestParam Long sysId, @RequestParam(required = false) Long createId,
				@RequestParam(required = false) Long updateId, @RequestParam Integer alterPoint, @RequestParam IntegralType integralType, @RequestParam(required = false) String remark)
				throws CustomException {
			
			Result<Map<String, Object>> result = new Result<>();
			integralRecordService.addIntegralRecord(memberId, sysId, createId, updateId, alterPoint, integralType, remark, "", "");
			result.setErrorCode(ErrorCode.SUCCESS);
			result.setData(null);
			return result;
		}*/
	
	// @Json(type = Result.class, exclude = "data,errorCode")
	/*@RequestMapping(value = "/updateBalance", method = RequestMethod.POST)
	@ApiOperation(value = "余额变更接口-wuyicheng", notes = "余额变更接口-wuyicheng")
	@Valids({ @Valid(name = "memberId", minValue = 1), @Valid(name = "sysId", minValue = 1), @Valid(name = "alterMoney", regex = RegexUtils.DOUBLE),
			@Valid(name = "balanceType", regex = "PAY|GIFT_CARD|REFUND|BUY_USE"), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "memberId", value = "会员ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "createId", value = "创建人ID", required = false, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "updateId", value = "修改人ID", required = false, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "alterMoney", value = "修改的金额", required = true, dataType = "bigDecimal", paramType = "query"),
			@ApiImplicitParam(name = "balanceType", value = "余额类型:PAY|GIFT_CARD|REFUND|BUY_USE;充值，礼品卡充值（醉品），退款转充值（醉品）购物使用充值", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "remark", value = "备注", required = false, dataType = "string", paramType = "query"), })
	
	public Result<Map<String, Object>> updateBalance(@RequestParam Long memberId, @RequestParam Long sysId, @RequestParam(required = false) Long createId,
			@RequestParam(required = false) Long updateId, @RequestParam BigDecimal alterMoney, @RequestParam BalanceType balanceType,
			@RequestParam(required = false) String remark) throws CustomException {
		
		Result<Map<String, Object>> result = new Result<>();
		balanceRecordService.updateBalance(memberId, sysId, createId, updateId, alterMoney, balanceType, remark);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setData(null);
		return result;
	}*/
	
}
